package com.xyht.sca_s.student_manage_system.modules.user.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.xyht.sca_s.student_manage_system.common.util.HttpTool;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SMSEntity.SendMultiContentReq;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SMSEntity.SendMultiRequestReq;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SMSEntity.SendRequestReq;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.xyht.sca_s.student_manage_system.modules.user.constant.commonConstant.ACCESS_KEY_ID;
import static com.xyht.sca_s.student_manage_system.modules.user.constant.commonConstant.SECRET;

public class SMSUtil {
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId(ACCESS_KEY_ID)
                // 您的 AccessKey Secret
                .setAccessKeySecret(SECRET);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * @description  阿里短信服务
     *
     * @create 2024/6/25
     * @author dxn
    */
    public static Boolean sendSMSCode(String tel,String code) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = createClient("accessKeyId", "accessKeySecret");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("星源慧通")
                .setTemplateCode("SMS_464106207")
                .setPhoneNumbers(tel)
                .setTemplateParam("{code:"+code+"}");
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(response));
            Map<String,Object> body = JSONObject.parseObject(jsonObject.getString("body"),Map.class);
            if (!body.get("code").equals("OK")){
                return false;
            }
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return true;
    }


    /**
     * @description  短信服务
     *
     * @create 2024/6/25
     * @author dxn
     */
    public static String Send_Post(String username){
        String url = "https://api.028lk.com/Sms/Api/Send";
        SendRequestReq requestDto = new SendRequestReq();
        requestDto.SecretName="SCSK00034524";
        requestDto.SecretKey="sc524@625";
        requestDto.Mobile="15892317881";
        requestDto.Content="亲爱的"+username+"同学:\n" +
                "      恭喜您已被我校录取！\n" +
                "      新生暂定于2024年8月25日8:00开始办理入学手续，请带上录取通知书、身份证及复印件、日常生活用品。欢迎您的到来！\n" +
                "      报到地址:自贡市自流井区光大街郭家坳顺龙坝165号(公交车6、8、15、303、305路可达)。\n" +
                "      如有疑问请联系:0813-2621598\n" +
                "      祝您及家人生活愉快！\n" +
                "               四川自贡市高级技工学校";
//        requestDto.TemplateId="模板ID";
//        requestDto.ExtCode="扩展号";
//        requestDto.SignName="短信签名";
//        requestDto.Timing="定时时间";
//        requestDto.CustomId="自定义ID";
        String data = JSON.toJSONString(requestDto);
        return  HttpTool.doPost(url,data);
    }

    /**
     * @description  录取通知短信
     *
     * @create 2024/6/25
     * @author dxn
     */
    public static String SendAdmission(String tel,String username){
        String url = "https://api.028lk.com/Sms/Api/Send_Multi";
        SendMultiRequestReq requestDto = new SendMultiRequestReq();
        requestDto.setSecretName("SCSK00034524");
        requestDto.setSecretKey("sc524@625");

        List<SendMultiContentReq> multiContents = new ArrayList<>();
        SendMultiContentReq content = new SendMultiContentReq();
        content.setMobile(tel);
        content.setTemplateId("8806");
        content.setSignName("【四川自贡市高级技工学校】");

        List<String> templateVars = new ArrayList<>();
        templateVars.add(username);
        content.setTemplateVars(templateVars);

        multiContents.add(content);

        requestDto.setMultiContents(multiContents);

        String data = JSON.toJSONString(requestDto);
        return  HttpTool.doPost(url,data);
    }

    /**
     * @description  登录验证码
     *
     * @create 2024/6/25
     * @author dxn
     */
    public static boolean sendVerifyCode(String tel,String code){
        String url = "https://api.028lk.com/Sms/Api/Send_Multi";
        SendMultiRequestReq requestDto = new SendMultiRequestReq();
        requestDto.setSecretName("SCSK00034524");
        requestDto.setSecretKey("sc524@625");

        List<SendMultiContentReq> multiContents = new ArrayList<>();
        SendMultiContentReq content = new SendMultiContentReq();
        content.setMobile(tel);
        content.setTemplateId("8815");
        content.setSignName("【四川自贡市高级技工学校】");

        List<String> templateVars = new ArrayList<>();
        templateVars.add(code);
        content.setTemplateVars(templateVars);

        multiContents.add(content);

        requestDto.setMultiContents(multiContents);

        String data = JSON.toJSONString(requestDto);
        JSONObject result = JSONObject.parseObject(HttpTool.doPost(url,data));
        return result.getInteger("code").equals(0);
    }

}
